layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 管理
     */
    var CbCustomerCompany = {
        tableId: "cbCustomerCompanyTable"
    };

    /**
     * 初始化表格的列
     */
    CbCustomerCompany.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: false, title: '企业ID'},
            // {field: 'logo', sort: true, title: 'logo'},
            {field: 'name', sort: true, title: '名称'},
            {field: 'phone', sort: true, title: '电话'},
            {field: 'scale', sort: true, title: '企业规模'},
            {field: 'nature', sort: true, title: '企业性质'},
            // {field: 'industryNo', sort: true, title: '所属行业'},
            {field: 'industryName', sort: true, title: '行业名称'},
            {field: 'city', sort: true, title: '企业所在地'},
            {field: 'addressDetail', sort: true, title: '详细地址'},
            {field: 'status', sort: true, title: '状态', templet: function (obj) {
                //1：提交(审核中) 2：通过（完成）3：驳回
                if (obj.status==1){
                    return '提交';
                }else if (obj.status==2){
                    return '通过';
                }else {
                    return '驳回';
                }
            }},
            // {field: 'introduction', sort: true, title: '企业简介'},
            {field: 'createTime', sort: true, title: '创建时间'},
            // {field: 'createUser', sort: true, title: '创建人'},
            // {field: 'updateTime', sort: true, title: '更新时间'},
            // {field: 'updateUser', sort: true, title: '更新人'},
            // {field: 'version', sort: true, title: '乐观锁'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    CbCustomerCompany.search = function () {
        var queryData = {};

        queryData['name'] = $('#name').val();
        queryData['phone'] = $('#phone').val();
        // queryData['scale'] = $('#scale').val();
        // queryData['nature'] = $('#nature').val();
        // queryData['industryNo'] = $('#industryNo').val();
        // queryData['city'] = $('#city').val();
        queryData['status'] = $('#status').val();
        // queryData['createTime'] = $('#createTime').val();
        // queryData['createUser'] = $('#createUser').val();

        table.reload(CbCustomerCompany.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    CbCustomerCompany.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/api/customer_company/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    CbCustomerCompany.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/api/customer_company/edit?id=' + data.id
    };

    /**
     * 点击审核
     *
     * @param data 点击按钮时候的行数据
     */
    CbCustomerCompany.jumpReviewPage = function (data) {
        window.location.href = Feng.ctxPath + '/api/customer_company/review?id=' + data.id
    };

    /**
     * 点击详情
     *
     * @param data 点击按钮时候的行数据
     */
    CbCustomerCompany.jumpDetailPage = function (data) {
        window.location.href = Feng.ctxPath + '/api/customer_company/detail_p?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    CbCustomerCompany.exportExcel = function () {
        var checkRows = table.checkStatus(CbCustomerCompany.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    CbCustomerCompany.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/api/customer_company/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(CbCustomerCompany.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + CbCustomerCompany.tableId,
        url: Feng.ctxPath + '/api/customer_company/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: CbCustomerCompany.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        CbCustomerCompany.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    CbCustomerCompany.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        CbCustomerCompany.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + CbCustomerCompany.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            CbCustomerCompany.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            CbCustomerCompany.onDeleteItem(data);
        }else if (layEvent == 'review'){
            CbCustomerCompany.jumpReviewPage(data);
        }else if (layEvent == 'detail'){
            CbCustomerCompany.jumpDetailPage(data);
        }
    });
});
